package com.wandian.texas.dao.Impl;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import com.wandian.texas.dao.ChatrecordDao;
import com.wandian.texas.db.DBMgr;
import com.wandian.texas.db.DbParameter;
import com.wandian.texas.db.WandianBaseDao;
import com.wandian.texas.entity.ChatrecordEntity;
import com.wandian.util.Log;

/**
 * @author long
 * @Create 2012-10-8 上午9:38:46
 * @QQ 108232706
 * @version 1.0
 * @description ChatrecordDaoImpl
 */
public class ChatrecordDaoImpl extends WandianBaseDao implements ChatrecordDao {

	public ChatrecordDaoImpl(DBMgr _dbMgr) {
		super(_dbMgr);
	}

	public ChatrecordEntity getChatrecordById(int id) {
		return null;
	}

//	@Override
//	protected Connection openConn()
//	{
//		return this.dbMgr.getDbWandian();
//	}

	public ChatrecordEntity getChatrecord(int Id) {
		String sqlText = "select * from t_Chatrecord WHERE  userId=? ;";
		Map<Integer, DbParameter> params = new HashMap<Integer, DbParameter>();
		params.put(1, new DbParameter(Types.INTEGER, Id));
		PreparedStatement pstmt = this.execQuery(sqlText, params);
		ResultSet rs = null;
		ChatrecordEntity info = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if (rs.last()) {
					info = new ChatrecordEntity();
					info.setId(Id);
					info.setUserId(rs.getInt("UserId"));
					info.setType(rs.getShort("Type"));
					info.setMessage(rs.getString("Message"));
				}
			} catch (SQLException e) {
				info = null;
				Log.error("执行出错" + sqlText, e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return info;
	}

	public boolean addChatrecord(ChatrecordEntity info) {
		boolean result = false;
		if (info == null) {
			return result;
		}
		if (info.beginAdd()) {
			String sql = " INSERT INTO t_Chatrecord(UserId, Type,Message, CreateDate)  VALUES(?,?, ?, ?);";
			Map<Integer, DbParameter> paraAdd = new HashMap<Integer,
					DbParameter>();
			paraAdd.put(1, new DbParameter(Types.INTEGER, info.getUserId()));
			paraAdd.put(2, new DbParameter(Types.TINYINT, info.getType()));
			paraAdd.put(3, new DbParameter(Types.VARCHAR, info.getMessage()));
			paraAdd.put(4, new DbParameter(Types.DATE, new Date(System.currentTimeMillis())));
			result = execNoneQuery(sql, paraAdd) > -1;
			info.commitAdd(result);
		}
		return result;
	}

	public boolean addChatrecord2(ChatrecordEntity info) {
		boolean result = false;
		if (info == null) {
			return result;
		}
		if (info.beginAdd()) {
			String sql = " INSERT INTO t_Chatrecord(UserId, Type,Message, CreateDate)  VALUES(?,?, ?, ?);";
			result = execNoneQuery2(sql,DBMgr.TexasPokerDB, info.getUserId(), info.getType(), info.getMessage(), new Date(System.currentTimeMillis())) > -1;
			info.commitAdd(result);
		}
		return result;
	}

	public ChatrecordEntity add(ChatrecordEntity info) {
		int result;
		if (info == null) {
			return info;
		}
		if (info.beginAdd()) {
			String sql = " INSERT INTO t_Chatrecord(UserId, Type,Message, CreateDate)  VALUES(?,?, ?, ?);";
			Map<Integer, DbParameter> paraAdd = new HashMap<Integer,
					DbParameter>();
			paraAdd.put(1, new DbParameter(Types.INTEGER, info.getUserId()));
			paraAdd.put(2, new DbParameter(Types.TINYINT, info.getType()));
			paraAdd.put(3, new DbParameter(Types.VARCHAR, info.getMessage()));
			paraAdd.put(4, new DbParameter(Types.DATE, new Date(System.currentTimeMillis())));
			result = super.execLastId(sql, paraAdd);
			info.setId(result);
			info.commitAdd(result > 0);
		}
		return info;
	}

	public ChatrecordEntity add2(ChatrecordEntity info) {
		int result;
		if (info == null) {
			return info;
		}
		if (info.beginAdd()) {
			String sql = " INSERT INTO t_Chatrecord(UserId, Type,Message, CreateDate)  VALUES(?,?, ?, ?);";
			result = super.execLastId2(sql, info.getUserId(), info.getType(), info.getMessage(), new Date(System.currentTimeMillis()));
			info.setId(result);
			info.commitAdd(result > 0);
		}
		return info;
	}

	public ChatrecordEntity batchAdd(ChatrecordEntity info, int count) {
		int result;
		if (info == null) {
			return info;
		}
		//		1) ps.setDate(1,new java.sql.Date(System.currentTimemillis())); 
		//		2) ps.setTime(2,new java.sql.Time(System.currentTimemillis())); 
		//		3) ps.setTimestamp(3,new java.sql.Timestamp(System.currentTimemillis())); 
		//		第一种只插入年月日 0000-00-00 
		//		第二种只插入时间 00:00:00 
		//		第三种则插入完整的时间 0000-00-00 00:00:00.000 .000是毫秒数
		//		1) getDate(String colname); // 取出日期 格式：0000-00-00 
		//		2) getTimestamp(String colname); // 取出日期和时间 格式：0000-00-00 00:00:00.000 
		//		3) getTime(String colname); // 只取出时间部份 格式：00:00:00 几乎不用，因为没有人只会取出时间，而不用日期。
		if (info.beginAdd()) {
			String sql = " INSERT INTO t_Chatrecord(UserId, Type,Message, CreateDate)  VALUES(?,?, ?, ?);";
			super.sqlBatch(sql, count, info.getUserId(), info.getType(), info.getMessage(), new Timestamp(System.currentTimeMillis()));
			info.commitAdd(true);
		}
		return info;
	}

	public ChatrecordEntity batchAdd2(ChatrecordEntity info, int count) {
		int result;
		if (info == null) {
			return info;
		}
		if (info.beginAdd()) {
			String sql = " INSERT INTO t_Chatrecord(UserId, Type,Message, CreateDate)  VALUES(?,?, ?, ?);";
			result = super.execLastId2(sql, info.getUserId(), info.getType(), info.getMessage(), new Date(System.currentTimeMillis()));
			info.setId(result);
			info.commitAdd(result > 0);
		}
		return info;
	}

	public boolean updateChatrecord(ChatrecordEntity info) {
		boolean result = false;
		if (info == null) {
			return result;
		}
		// if (info.beginUpdate()) {
		// String sql =
		// "  UPDATE t_Chatrecord SET  `RealName`=?   Where `UserId`=? And `UserName`=?; ";
		// Map<Integer, DbParameter> paraUpdate = new HashMap<Integer,
		// DbParameter>();
		// paraUpdate.put(1, new DbParameter(4, info.getRealName()));
		// paraUpdate.put(2, new DbParameter(4, info.getUserId()));
		// paraUpdate.put(3, new DbParameter(4, info.getUserName()));
		// result = execNoneQuery(sql, paraUpdate) > -1;
		// info.commitUpdate(result);
		// }
		return result;
	}

	public boolean delChatrecord(int Id) {
		boolean result = false;
		String sql = "delete from t_Chatrecord where Id=?; ";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(4, Id));
		result = execNoneQuery(sql, para) > -1;
		return result;
	}

}
